package com.aftersale.demo.service.impl;

import com.aftersale.demo.entity.*;
import com.aftersale.demo.filter.JwtAuthenticationTokenFilter;
import com.aftersale.demo.utils.RedisCache;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.aftersale.demo.service.RolePermissionService;
import com.aftersale.demo.mapper.RolePermissionMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 *
 */
@Service
public class RolePermissionServiceImpl extends ServiceImpl<RolePermissionMapper, RolePermission>
    implements RolePermissionService{
    @Autowired
    RolePermissionMapper rolePermissionMapper;
    @Override
    public Result manageRolePermission(Role role) {
        LambdaQueryWrapper<RolePermission> deleteWrapper=new LambdaQueryWrapper<>();
        deleteWrapper.eq(RolePermission::getRoleId,role.getId());
        rolePermissionMapper.delete(deleteWrapper);
        List<Permission> permissions=role.getRolePermission();
        if(Objects.isNull(permissions)||permissions.size()==0){
            rolePermissionMapper.manageRolePermission(role.getId(),19);
        }else{
            for (Permission permission : permissions) {
                rolePermissionMapper.manageRolePermission(role.getId(),permission.getId());
            }
        }
//        String userId=JwtAuthenticationTokenFilter.getUserId();
//        LoginUser loginUser=redisCache.getCacheObject("login"+userId);
//        String redisRoleId=loginUser.getUser().getRoleId().toString();
//        if(role.getId().toString().equals(redisRoleId)){
//            redisCache.deleteObject("login"+userId);
//            System.out.println("test");
//        }
        return Result.success("配置成功");
    }
}




